//
//  RegisterViewController.m
//  app1
//
//  Created by 张领瑞 on 2017/8/29.
//  Copyright © 2017年 ShiFeiEr.     All rights reserved.
//

#import "RegisterViewController.h"

#import "PrefixHeader.pch"
@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;

@property (weak, nonatomic) IBOutlet UITextField *passwordTF;

@property (weak, nonatomic) IBOutlet UIButton *codeBT;

@end

@implementation RegisterViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addTapGestureForViewControllerToRecoverKeyboard];//添加手势回收键盘
    // Do any additional setup after loading the view.
}
#pragma mark 获取验证码
- (IBAction)getCodeAction:(id)sender {
    
    if ([self.phoneNumTF isMobilePhoneNumber]) {//填写的phonenumber正确
        [self.codeBT leftSecondsShow];
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneNumTF.text zone:@"86" result:^(NSError *error) {
            if (error) {//获取验证码失败
                [self presentAlertViewWithControlTitle:@"提示" Message:@"获取验证码失败，请稍后再试！" ActionTitle:@"确认"];
            }
    }];
    }else{//填写的phonenumber错误
        [self presentDefaultAlertViewWithMessage:@"请正确输入手机号码"];
    }
    
}

#pragma mark 注册账号
- (IBAction)registAction:(id)sender {
    
    
    if (![self.phoneNumTF isMobilePhoneNumber]) {//不是移动电话号
        [self presentDefaultAlertViewWithMessage:@"请正确输入手机号码"];
        return;
    }
    if (!self.codeTF.text.length) {//没有输入验证码
        [self presentDefaultAlertViewWithMessage:@"请输入验证码"];
        return;
    }
    if ((_passwordTF.text.length < 6 && _passwordTF.text.length > 16)) {
        
        [self presentDefaultAlertViewWithMessage:@"请确认您输入的密码长度在6-16位"];
        return;
    }else{//符合注册
        
        
        [SMSSDK commitVerificationCode:self.codeTF.text phoneNumber:self.phoneNumTF.text zone:@"86" result:^(NSError *error) {
            
            if (error) {
                [self presentDefaultAlertViewWithMessage:@"验证失败，请稍后再试!"];
                return ;
            }
            
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
            [dic setObject:self.phoneNumTF.text forKey:@"phoneNumber"];
            NSString *MD5Str = [UIViewController MD5ForLower32Bate:self.passwordTF.text];
            [dic setObject:MD5Str forKey:@"password"];
            
           AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
           [manager POST:kREGISTEURL parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

                NSLog(@"注册成功---%@",responseObject);
                [self presentDefaultAlertViewWithMessage:@"完成注册"];
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"注册失败～～～～%@",error.description);
                [self presentDefaultAlertViewWithMessage:@"注册失败"];
            }];
        }];
   }
}
#pragma mark 返回登陆界面
- (IBAction)backToLoginVC:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark app协议
- (IBAction)appAgreement:(id)sender {
    
    SafetyNoticeViewController *safetyNoticeVC = [[SafetyNoticeViewController alloc] init];
    
    
    [self presentViewController:safetyNoticeVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
